package com.example.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.example.demo.entity.DTO.WorkersAndBookedTimeDTO;
import com.example.demo.entity.VO.WorkersAndBookedTimeVO;
import com.example.demo.result.Result;
import com.example.demo.service.IBookedOrdersService;
import com.example.demo.service.IUserService;
import com.example.demo.service.impl.BookedOrdersServiceImpl;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import com.example.demo.service.IWorkersAndBookedTimeService;
import com.example.demo.entity.WorkersAndBookedTime;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 员工预约表 前端控制器
 * </p>
 *
 * @author Mr.Xiayh
 * @since 2024-05-31
 */
@Slf4j
@Api(tags = "预约接口")
@RestController
@RequestMapping("/workers-booked")
public class WorkersBookedController {

    @Resource
    private IWorkersAndBookedTimeService workersAndBookedTimeService;

    @Resource
    private IBookedOrdersService bookedOrdersService;

    @Resource
    private IUserService userService;
    /**
     * 以需求查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Xiayh")
    @ApiOperation(value = "以需求查询所有接口")
    @PostMapping("/findAllByCriteria")
    public Result<List<WorkersAndBookedTimeDTO>> findAllByCriteria(@RequestBody WorkersAndBookedTimeVO workersAndBookedTimeVO) {
        WorkersAndBookedTime workersAndBookedTime = new WorkersAndBookedTime();
        BeanUtil.copyProperties(workersAndBookedTimeVO,workersAndBookedTime);
        List<WorkersAndBookedTimeDTO> byCriteria = workersAndBookedTimeService.getByCriteria(workersAndBookedTime);
        return Result.success().msg("查询成功").data(byCriteria);
    }

    /**
     * 预约护理师
     * @return
     */
    @ApiOperationSupport(author = "Mr.sdy")
    @ApiOperation(value = "预约护理师")
    @PostMapping("/booked")
    public Result booked(int wbt_id,int userId) {
        //TODO 登录功能实现了 参数需要添加userID
//        String tokenValue = StpUtil.getTokenValue();
//        int userId = (int) StpUtil.getLoginIdByToken(tokenValue);
        try {
            // 检查所选时间段是否已被预订，如果预订数量为0或更少，则表示该时间段已无空位
            if (workersAndBookedTimeService.findBookedTime(wbt_id).getBookedNum() <= 0) {
                return Result.error().msg("预约失败，该时间段已无空位");
            }
        } catch (NullPointerException e) {
            // 如果查询时间段时发生NullPointerException，说明该时间段不存在
            return Result.error().code(99403).msg("预约失败，该时间段不存在");
        }

        // 验证用户是否存在，如果用户不存在，则返回错误信息
        if (userService.getById(userId) == null) {
            return Result.error().code(99403).msg("用户不存在");
        }
        // 尝试预订所选时间段，如果成功，则尝试插入预约订单
        if (workersAndBookedTimeService.bookedWorker(wbt_id)) {
            // 尝试插入预约订单
            if (bookedOrdersService.insertOrde(wbt_id, userId)) {
                // 如果插入订单成功，则返回预约成功的信息
                return Result.success().msg("预约成功");
            }
        }
        // 如果预订或插入订单失败，则返回预约失败的信息
        return Result.error().code(99042).msg("预约失败");
    }

    /**
     * 取消预约护理师（删除预约订单）
     * @return
     */
    @ApiOperationSupport(author = "Mr.sdy")
    @ApiOperation(value = "取消预约护理师")
    @PostMapping("/cancelBooked")
    public Result cancelBooked(int borderId,int wbt_id) {
        if(bookedOrdersService.deleteOrder(borderId)){
            if(workersAndBookedTimeService.cancelBooked(wbt_id)){
                return Result.success().msg("取消预约成功");
            }
        }
        return Result.error().msg("取消预约失败");
    }

}